$(function(){
	parent.IndexPage.toggleLocker('close'); // 沉浸式体验
	$("#searchArticle").on('keydown', function(event) {
		if (event.keyCode == 13) {
			// 回车
			var originalName = $("#searchArticle").val();
			$("#findFileInfoFrm").find("input[name='originalName']").val(originalName);
			findFileInfoAjax.send();
		}
		if (event.keyCode == 27) {
			// ESC
			var originalName = $("#searchArticle").val();
			if (StringUtil.isNotBlank(originalName)) {
				$("#searchArticle").val('');
				$("#findFileInfoFrm").find("input[name='originalName']").val('');
				findFileInfoAjax.send();
			}
		}
	});
});
var FileListPage = {
	'onFileChanged':function(ele) {
		
	},
	'onUploadSucc':function(stat) {
		stat = JSON.parse(stat);
		if(stat.success == true){
			$.messager.info("文件上传成功！");
			fileInfoDatatable.refresh();
		} else {
			$.messager.error("文件上传失败！");
		}
	},
	'onQuerySucc':function(data, status, xhr) {
		
	},
	// 删除文件
	'deleteFile':function(item, e, rowNumber) {
		MsgBox.confirm("确认删除文件：" + item.originalName + "?", null, function() {
			fileDeleteAjax.tempUrl(function(url){
				return url + "/" + item.fileId;
			});
			fileDeleteAjax.send();
		});
	},
	// 文件删除成功
	'onFileDeleteSucc':function(data, status, xhr) {
		$.messager.info(data.message);
		var mWindow = MsgBox.getCurrentOpenDialogWindow();
		mWindow.fileInfoDatatable.refresh();
	},
	// 下载文件
	'downloadFile':function(item, e, rowNumber) {
		$.download({
			'data':{
				'fileId':item.fileId,
				'download':true
			},
			'url':ctx + "/datum/file/fileDownload"
		});
		FileListPage.updateDownloadCount(item.fileId, rowNumber); // 更新下载次数
	},
	// 通过超链接下载文件，更新下载次数
	'updateDownloadCount':function(fileId, rowNumber) {
		updateDownloadCountAjax.tempUrl(function(url){
			return url + "/" + fileId + "/" + rowNumber;
		});
		updateDownloadCountAjax.send();
	},
	// 更新下载次数成功
	'onUpdateDownloadCountSucc':function(data, status, xhr) {
		var obj = data.info; // obj.fileId, obj.row
		var row = obj.row;
		var $downloadCount = $("#fileInfoDatatable").find("tr[row-index='" + row + "']").find("td[column-key='downloadCount'] p");
		$downloadCount.text(parseInt($downloadCount.text()) + 1);
	},
	// 获取url
	'getUrl':function(mItem) {
		return blogPath + "/files/" + mItem.fileId + "." + FilenameUtils.getExtension(mItem.originalName);
	},
	// 渲染每一项
	'onRenderItem':function(colName,text,indexStr,mItem, rowNumber) {
		switch(colName) {
		case 'visitLink':
			var $container = $("<div class='oper-btn-group'/>");
			var url = FileListPage.getUrl(mItem);
			var $input = $("<input value='" + url + "' data-tag='link' type='text' style='width: 350px; border: 1px solid #EEE;' readonly='readonly'>");
			var $icon = $("<button type='button' title='复制链接' onclick='FileListPage.copyLink(this)'><i class='fa fa-fw fa-copy'></i></button>");
			var $folder = $("<button type='button' title='打开文件夹' data-id='" + mItem.fileId + "' onclick='FileListPage.openFolder(this)'><i class='fa fa-fw fa-folder-o'></i></button>");
			var $file = $("<button type='button' title='打开文件' data-id='" + mItem.fileId + "' onclick='FileListPage.openFile(this)'><i class='fa fa-fw fa-file-o'></i></button>");
			$container.append($input);
			$container.append($icon);
			if (mItem.nativeTag == '1') {
				$container.append($folder);
				$container.append($file);
			}
			return $container;
		case 'originalName':
			return $.buildDatatableInput({
                'name': 'originalName',
                'rowNumber': rowNumber,
                'id': mItem.fileId,
                'okFunc': FileListPage.updateOriginalName,
                'text': text,
                'onEditText': (function(text) {
                	if (!StringUtil.isBlank(text)) {
                		return text.substring(0, text.lastIndexOf("."));
                	}
                	return text;
                })(text),
                'width': '360px'
            });
		}
	},
	// 查看二维码
	'viewQrcode':function(item, e, rowNumber) {
		var text = BASE64.encoder(FileListPage.getUrl(item));
		MsgBox.open("viewQrcode", "查看二维码", ctx + "/datum/qrcode/" + text, '400px', null, null, 'no-footer');
	},
	// 修复文件大小
	'repairFileSize':function(item, e, rowNumber) {
		repairFileSizeAjax.tempUrl(function(url) {
			return url + item.fileId;
		}).send();
	},
	'onRepairSizeSucc':function(data, status, xhr) {
		$.messager.info(data.message);
		fileInfoDatatable.refresh();
	},
	// 复制链接
	'copyLink': function(ele) {
		var inputEle = $(ele).parent().find("input[data-tag='link']")[0];
		inputEle.select();
		document.execCommand("Copy");
		$.messager.info('已成功复制到粘贴板！');
	},
	// 修改文件名
	'updateOriginalName': function(id, value) {
		if (StringUtil.isBlank(value)) {
			$.messager.info("文件名不能为空！");
			return false;
		}
		if (value.length > 200) {
			$.messager.info("文件名长度过长！");
			return false;
		}
		renameAjax.tempUrl(function(url) {
			return url + id;
		}).setData({
			'fileName': value
		}).send();
	},
	// 修改成功
	'updateSucc': function(data, status, xhr) {
		$.messager.info(data.message);
		fileInfoDatatable.refresh(false);
	},
	// 打开文件夹
	'openFolder': function(ele) {
		var fileId = $(ele).data('id');
		openFolderAjax.tempUrl(function(url) {
			return url + fileId;
		}).send();
	},
	'openFile': function(ele) {
		var fileId = $(ele).data('id');
		openFileAjax.tempUrl(function(url) {
			return url + fileId;
		}).send();
	},
	'onOpenSucc': function(data, status, xhr) {
		// $.messager.info(data.message);
	}
};